import { MessageBox } from 'element-ui'
import store from 'Plugins/store'
import router from 'Plugins/router'

let confirmEnable = true
const responseSuccessHandler = function (response) {
  let res = response.data
  if (res && res.error_code === '***') {
    if (confirmEnable) {
      confirmEnable = false
      MessageBox.confirm('登录失效，请重新登录！', {
        title: '提示',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          store.commit('user/SET_USER_INFO', null)
          store.commit('user/SET_SITES', null)
          localStorage.clear()
          router.push({ name: 'login' })
          confirmEnable = true
        })
        .catch(() => { confirmEnable = true })
    }
  }
  return Promise.resolve(response)
}

export default {
  responseSuccessHandler
}
